﻿using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.Xna.Framework;

using AimGameLibrary.GameObject;
namespace AimGameLibrary
{
    
    public class Camera : BaseGameObject
    {
        RunMode myMode;
        public RunMode MymodE { set { myMode = value; } }

        Point Resolution;
        public Point ResolutioN { set { Resolution = value; } }
        
        public Camera(String InputName)
        {
            MyViewData = new ViewData();
            MyViewData.Name = InputName;
            MyViewData.Position = Vector2.Zero;
            MyViewData.Rotation = 0.0f;
            MyViewData.Origin = Vector2.Zero;
            MyViewData.Scale = Vector2.One;
            Resolution = Point.Zero;
            myMode = RunMode.PreViewMode;
        }

        public override void LoadData(Identification getdata, int IndexData, NodeData node)
        {
            base.LoadData(getdata, IndexData, node);

            InputMode = (NodeInputMode)getdata.Items.GetValue(IndexData + 1);
            if (InputMode == NodeInputMode.None)
            {
                MyViewData.Position = (Vector2)getdata.Items.GetValue(IndexData + 2);
                MyViewData.Scale = (Vector2)getdata.Items.GetValue(IndexData + 3);
                MyViewData.Rotation = (float)getdata.Items.GetValue(IndexData + 4);
            }
            else
            {
                String Temp = node.Data;
                Listnameparent.Add(Script.GetName(Temp, 3, 0));
                Listnameparent.Add(Script.GetName(Temp, 3, 1));
                Listnameparent.Add(Script.GetName(Temp, 3, 2));
            }
        }

        public Vector2 ApplyPosition(Image2D i)
        {
            Vector2 iPos = Vector2.Zero;

            if(myMode == RunMode.PreViewMode)
                iPos = i.MyViewData.Position + (i.MyViewData.Origin * i.MyViewData.Scale) + MyViewData.Position;
            else if(myMode == RunMode.GamePlayMode)
                iPos = i.MyViewData.Position + (i.MyViewData.Origin * i.MyViewData.Scale) - MyViewData.Position;
            else iPos = i.MyViewData.Position + (i.MyViewData.Origin * i.MyViewData.Scale) + MyViewData.Position;
           
            iPos = new Vector2(
                   (float)(iPos.X * Math.Cos(MyViewData.Rotation) - iPos.Y * Math.Sin(MyViewData.Rotation))
                 , (float)(iPos.X * Math.Sin(MyViewData.Rotation) + iPos.Y * Math.Cos(MyViewData.Rotation))
                 );

            // Scale muliply
            Vector2 temp = (iPos) * MyViewData.Scale + MyViewData.Origin;
            return temp;
        }

        public Vector2 ApplyPosition(Vector2 i)
        {
            Vector2 iPos = i + MyViewData.Position;
            iPos = new Vector2(
                   (float)(iPos.X * Math.Cos(MyViewData.Rotation) - iPos.Y * Math.Sin(MyViewData.Rotation))
                 , (float)(iPos.X * Math.Sin(MyViewData.Rotation) + iPos.Y * Math.Cos(MyViewData.Rotation))
                 );

            // Scale muliply
            Vector2 temp = (iPos) * MyViewData.Scale + MyViewData.Origin;
            return temp;
        } 

        public Vector2 ApplyScale(Image2D i)
        {
            // None Appact
            return Vector2.One * i.MyViewData.Scale;
        }

        public override void UpdateGameTime(GameTime gametime)
        {
            base.UpdateGameTime(gametime);
            if(Listparent.Count > 0)
            {
                if (Listparent[0].GetType() == typeof(GameVector2) || Listparent[0].GetType() == typeof(GameTakeCamera))
                { 
                    GameVector2 gameposition = (GameVector2)Listparent[0];
                    MyViewData.Position = gameposition.Myvector2;
                }
            }
            
        }
    }
}
